#include "DispatcherHandler.h"
#include "DataStorageIO.h"
#include "log.h"

#define SAMPLE_INTERVAL_MS 2
#define OPERATE_SYMBOL_LT   0
#define OPERATE_SYMBOL_EQ   1
#define OPERATE_SYMBOL_GT   2
#define OPERATE_SYMBOL_LE   3
#define OPERATE_SYMBOL_GE   4
#define OPERATE_SYMBOL_NE   5

DispatcherHandler::DispatcherHandler(IOPort& ioport,ti::SN74AHC595& sn74ahc595,DataStorage& ds,PinName pin_int):
    _ioport(ioport),
    _sn74ahc595(sn74ahc595),
    _ds(ds),
    _interrupt_pin(pin_int)
    
{
    _workMode=WorkMode::Manual;
    _conditionActionName = ConditionActionName::CA0;
    _sample_interval_ms = SAMPLE_INTERVAL_MS;
    
}
DispatcherHandler::~DispatcherHandler()
{

}

void DispatcherHandler::begin()
{
    module_general_reg_t& general_reg = DataStorageIO::module_reg().module_general_reg;
    general_reg.mode_select_reg.value==0?_workMode=WorkMode::Manual:WorkMode::Auto;
    //设置电压
    _sn74ahc595.begin(general_reg.io_voltage_reg.data);
    _sn74ahc595.updateRegisters();
    //设置A0
    if(general_reg.io_direction_reg.a0 == 1){
        _ioport.aio(0).output();
        //设置类型
        _ioport.aio(0).write(general_reg.dio_level_reg.d0);
    }else{
        _ioport.aio(0).input();
    }
    //设置A1
    if(general_reg.io_direction_reg.a1 == 1){
        _ioport.aio(1).output();
        //设置类型
        _ioport.aio(1).write(general_reg.dio_level_reg.d1);
    }else{
        _ioport.aio(1).input();
    }
    //设置A2
    if(general_reg.io_direction_reg.a2 == 1){
        _ioport.aio(2).output();
        //设置类型
        _ioport.aio(2).write(general_reg.dio_level_reg.d2);
    }else{
        _ioport.aio(2).input();
    }
    //设置A3
    if(general_reg.io_direction_reg.a3 == 1){
        _ioport.aio(3).output();
        //类型LEVEL
        _ioport.aio(3).write(general_reg.dio_level_reg.d3);
    }else{
        _ioport.aio(3).input();
    }

    //设置D0
    if(general_reg.io_direction_reg.d0 == 1){
        _ioport.dio(0).output();
        //设置类型
        if(general_reg.io_type_reg.d0 == 1){
            _ioport.pwm0().set_frequency((PwmFreqName)general_reg.pwm_reg.freq.d01,general_reg.pwm_reg.duty_cycle.d0*1.0/100);
        }else{
            _ioport.dio(0).write(general_reg.dio_level_reg.d0);
        }
    }else{
        _ioport.dio(0).input();
    }
    //设置D1
    if(general_reg.io_direction_reg.d1 == 1){
        _ioport.dio(1).output();
        //设置类型
        if(general_reg.io_type_reg.d1 == 1){
            _ioport.pwm1().set_frequency((PwmFreqName)general_reg.pwm_reg.freq.d01,general_reg.pwm_reg.duty_cycle.d1*1.0/100);
        }else{
            _ioport.dio(1).write(general_reg.dio_level_reg.d1);
        }
    }else{
        _ioport.dio(1).input();
    }
    //设置D2
    if(general_reg.io_direction_reg.d2 == 1){
        _ioport.dio(2).output();
        //设置类型
        if(general_reg.io_type_reg.d2 == 1){
            _ioport.pwm2().set_frequency((PwmFreqName)general_reg.pwm_reg.freq.d23,general_reg.pwm_reg.duty_cycle.d2*1.0/100);
        }else{
            _ioport.dio(2).write(general_reg.dio_level_reg.d2);
        }
    }else{
        _ioport.dio(2).input();
    }
    //设置D3
    if(general_reg.io_direction_reg.d3 == 1){
        _ioport.dio(3).output();
        //设置类型
        if(general_reg.io_type_reg.d3 == 1){
            _ioport.pwm3().set_frequency((PwmFreqName)general_reg.pwm_reg.freq.d23,general_reg.pwm_reg.duty_cycle.d3*1.0/100);
        }else{
            _ioport.dio(3).write(general_reg.dio_level_reg.d3);
        }
    }else{
        _ioport.dio(3).input();
    }

    this->reset_work_mode(general_reg);
}

// int DispatcherHandler::apply_median_filter(AnalogIn & an)
// {
// 	int data[3];
//     for(uint8_t i=0;i<3;i++){
//         delay(_sample_interval_ms);
//         data[i] = an.read_u16();
//     }
//     return  (data[0] > data[1]) ? (data[1] > data[2] ? data[1] : (data[0] > data[2] ? data[2] : data[0])) : (data[0] > data[2] ? data[0] : (data[1] > data[2] ? data[2] : data[1]));
// }

//hanlde_condition_action
void DispatcherHandler::schedule()
{
    if(_workMode==WorkMode::Manual){
        return;
    }
    this->update();

}
void DispatcherHandler::update()
{
    module_general_reg_t& general_reg = DataStorageIO::module_reg().module_general_reg;
    
    switch(_conditionActionName)
    {
        case ConditionActionName::CA0:
        {
            
            if(general_reg.condition_action_enable_reg.ca0 && (general_reg.slave_interrupt_reg.ca0==0)){
                //log::debug("CA0:%d:%d\r\n",general_reg.condition_action_enable_reg.ca0,general_reg.slave_interrupt_reg.ca0);
                if(this->apply_condition_compare(0)){
                    this->apply_output_action(0);
                    //设置完成中断-CA0
                    general_reg.slave_interrupt_reg.ca0 = 1;
                    _interrupt_pin = 1;
                    log::debug("CA0 OK\r\n");
                }
            }
            _conditionActionName = ConditionActionName::CA1;
        }
        break;
        case ConditionActionName::CA1:
        {   
            
            if(general_reg.condition_action_enable_reg.ca1 && (general_reg.slave_interrupt_reg.ca1==0)){
                //log::debug("CA1:%d:%d\r\n",general_reg.condition_action_enable_reg.ca1,general_reg.slave_interrupt_reg.ca1);
                if(this->apply_condition_compare(1)){
                    this->apply_output_action(1);
                    //设置完成中断-CA1
                    general_reg.slave_interrupt_reg.ca1 = 1;
                    _interrupt_pin = 1;
                    log::debug("CA1 OK\r\n");
                }
            }
            _conditionActionName = ConditionActionName::CA2;
        }
        break;
        case ConditionActionName::CA2:
        {
           
            if(general_reg.condition_action_enable_reg.ca2 && (general_reg.slave_interrupt_reg.ca2==0)){
                 //log::debug("CA2:%d:%d\r\n",general_reg.condition_action_enable_reg.ca2,general_reg.slave_interrupt_reg.ca2);
                if(this->apply_condition_compare(2)){
                    this->apply_output_action(2);
                    //设置完成中断-CA2
                    general_reg.slave_interrupt_reg.ca2 = 1;
                    _interrupt_pin = 1;
                    log::debug("CA2 OK\r\n");
                }
            }
            _conditionActionName = ConditionActionName::CA3;
        }
        break;
        case ConditionActionName::CA3:
        {
            
            if(general_reg.condition_action_enable_reg.ca3 && (general_reg.slave_interrupt_reg.ca3==0)){
                //log::debug("CA3:%d:%d\r\n",general_reg.condition_action_enable_reg.ca3,general_reg.slave_interrupt_reg.ca3);
                if(this->apply_condition_compare(3)){
                    this->apply_output_action(3);
                    //设置完成中断-CA3
                    general_reg.slave_interrupt_reg.ca3 = 1;
                    _interrupt_pin = 1;
                    log::debug("CA3 OK\r\n");
                }
            }
            _conditionActionName = ConditionActionName::CA4;
        }
        break;
        case ConditionActionName::CA4:
        {
            
            if(general_reg.condition_action_enable_reg.ca4 && (general_reg.slave_interrupt_reg.ca4==0)){
                //log::debug("CA4:%d:%d\r\n",general_reg.condition_action_enable_reg.ca4,general_reg.slave_interrupt_reg.ca4);
                if(this->apply_condition_compare(4)){
                    this->apply_output_action(4);
                    //设置完成中断-CA4
                    general_reg.slave_interrupt_reg.ca4 = 1;
                    _interrupt_pin = 1;
                    log::debug("CA4 OK\r\n");
                }
            }
            _conditionActionName = ConditionActionName::CA5;
        }
        break;
        case ConditionActionName::CA5:
        {
            
            if(general_reg.condition_action_enable_reg.ca5 && (general_reg.slave_interrupt_reg.ca5==0)){
                //log::debug("CA5:%d:%d\r\n",general_reg.condition_action_enable_reg.ca5,general_reg.slave_interrupt_reg.ca5);
                if(this->apply_condition_compare(5)){
                    this->apply_output_action(5);
                    //设置完成中断-CA5
                    general_reg.slave_interrupt_reg.ca5 = 1;
                    _interrupt_pin = 1;
                    log::debug("CA5 OK\r\n");
                }
            }
            _conditionActionName = ConditionActionName::CA6;
        }
        break;
        case ConditionActionName::CA6:
        {
            
            if(general_reg.condition_action_enable_reg.ca6 && (general_reg.slave_interrupt_reg.ca6==0)){
                //log::debug("CA6:%d:%d\r\n",general_reg.condition_action_enable_reg.ca6,general_reg.slave_interrupt_reg.ca6);
                if(this->apply_condition_compare(6)){
                    this->apply_output_action(6);
                    //设置完成中断-CA6
                    general_reg.slave_interrupt_reg.ca6 = 1;
                    _interrupt_pin = 1;
                    log::debug("CA6 OK\r\n");
                }
            }
            _conditionActionName = ConditionActionName::CA7;
        }
        break;
        case ConditionActionName::CA7:
        {
            
            if(general_reg.condition_action_enable_reg.ca7 && (general_reg.slave_interrupt_reg.ca7==0)){
                //log::debug("CA7:%d:%d\r\n",general_reg.condition_action_enable_reg.ca7,general_reg.slave_interrupt_reg.ca7);
                if(this->apply_condition_compare(7)){
                    this->apply_output_action(7);
                    //设置完成中断-CA7
                    general_reg.slave_interrupt_reg.ca7 = 1;
                    _interrupt_pin = 1;
                    log::debug("CA7 OK\r\n");
                }
            }
            _conditionActionName = ConditionActionName::CA0;
        }
        break;
        default:
        {
            _conditionActionName = ConditionActionName::CA0;
        }
        break;
    }
}

void DispatcherHandler::reset_work_mode(module_general_reg_t& general_reg)
{
    _workMode= general_reg.mode_select_reg.value==0?WorkMode::Manual:WorkMode::Auto;
    if(_workMode == WorkMode::Auto){
        for(uint8_t i=0;i<8;i++){
            _logic_conditions[i].state = ActionState::Ready;
        }
    }
}
//委托SMB调用
void DispatcherHandler::dispatch(uint8_t* data,size_t size)
{
    //手动模式-写
    module_general_reg_t& general_reg = DataStorageIO::module_reg().module_general_reg;
    log::debug("mem:%x:%x:%x-%x:%x:%x\r\n",
                data[0],
                data[1],
                data[2],
                data[3],
                data[4],
                data[5]);
    switch(data[0]){
        case 0x40:
        {
            // 更新值
        }
        break;
        case 0x41://中断
        {
            general_reg.slave_interrupt_reg.value &= data[1];

            // for(uint8_t i=0;i<8;i++){
            //     auto_mode_logic_output_action_t& output_action_0 = general_reg.general_condition_and_action_reg.conditions[i].logic_output_action[0];
            //     if(output_action_0.en ){
            //         switch(general_reg.general_output_action_reg.actions[output_action_0.num].pin_name)
            //         {
            //             case 0:
            //             {
            //                 //0:点低电平，1:PWM
            //                 if(general_reg.general_output_action_reg.actions[output_action_0.num].output_type==0){
            //                     _ioport.dio0().write(general_reg.general_output_action_reg.actions[output_action_0.num].duty_cycle>0?0:1);
            //                 }else{
            //                     _ioport.pwm0().write(0);
            //                 }
            //             }
            //             break;
            //             case 1:
            //             {
            //                 //0:点低电平，1:PWM
            //                 if(general_reg.general_output_action_reg.actions[output_action_0.num].output_type==0){
            //                     _ioport.dio1().write(general_reg.general_output_action_reg.actions[output_action_0.num].duty_cycle>0?0:1);
            //                 }else{
            //                     _ioport.pwm1().write(0);
            //                 }
            //             }
            //             break;
            //             case 2:
            //             {
            //                 //0:点低电平，1:PWM
            //                 if(general_reg.general_output_action_reg.actions[output_action_0.num].output_type==0){
            //                     _ioport.dio2().write(general_reg.general_output_action_reg.actions[output_action_0.num].duty_cycle>0?0:1);
            //                 }else{
            //                     _ioport.pwm2().write(0);
            //                 }
            //             }
            //             break;
            //             case 3:
            //             {
            //                 //0:点低电平，1:PWM
            //                 if(general_reg.general_output_action_reg.actions[output_action_0.num].output_type==0){
            //                     _ioport.dio3().write(general_reg.general_output_action_reg.actions[output_action_0.num].duty_cycle>0?0:1);
            //                 }else{
            //                     _ioport.pwm3().write(0);
            //                 }
            //             }
            //             break;
            //             default:break;
            //         }
                    
            //     }
            //     //动作1输出
            //     auto_mode_logic_output_action_t& output_action_1 = general_reg.general_condition_and_action_reg.conditions[i].logic_output_action[1];
            //     if(output_action_1.en ){
            //         switch(general_reg.general_output_action_reg.actions[output_action_1.num].pin_name)
            //         {
            //             case 0:
            //             {
            //                 //0:点低电平，1:PWM
            //                 if(general_reg.general_output_action_reg.actions[output_action_1.num].output_type==0){
            //                     _ioport.dio0().write(general_reg.general_output_action_reg.actions[output_action_1.num].duty_cycle>0?0:1);
            //                 }else{
            //                     _ioport.pwm0().write(0);
            //                 }
            //             }
            //             break;
            //             case 1:
            //             {
            //                 //0:点低电平，1:PWM
            //                 if(general_reg.general_output_action_reg.actions[output_action_1.num].output_type==0){
            //                     _ioport.dio1().write(general_reg.general_output_action_reg.actions[output_action_1.num].duty_cycle>0?0:1);
            //                 }else{
            //                     _ioport.pwm1().write(0);
            //                 }
            //             }
            //             break;
            //             case 2:
            //             {
            //                 //0:点低电平，1:PWM
            //                 if(general_reg.general_output_action_reg.actions[output_action_1.num].output_type==0){
            //                     _ioport.dio2().write(general_reg.general_output_action_reg.actions[output_action_1.num].duty_cycle>0?0:1);
            //                 }else{
            //                     _ioport.pwm2().write(0);
            //                 }
            //             }
            //             break;
            //             case 3:
            //             {
            //                 //0:点低电平，1:PWM
            //                 if(general_reg.general_output_action_reg.actions[output_action_1.num].output_type==0){
            //                     _ioport.dio3().write(general_reg.general_output_action_reg.actions[output_action_1.num].duty_cycle>0?0:1);
            //                 }else{
            //                     _ioport.pwm3().write(0);
            //                 }
            //             }
            //             break;
            //             default:break;
            //         }
            //     }
            // }   
            
        }
        break;
        case 0x42://更新设置
        {
        }
        break;
        case 0x43://模式选择
        {
            general_reg.mode_select_reg.value = data[1];
            this->reset_work_mode(general_reg);
        }
        break;
        case 0x44://IO方向
        {
            general_reg.io_direction_reg.value = data[1];
            //D口IO初始化-方向
            general_reg.io_direction_reg.d0==0?pinMode(PD_3,INPUT):pinMode(PD_3,OUTPUT);
            general_reg.io_direction_reg.d1==0?pinMode(PB_1,INPUT):pinMode(PB_1,OUTPUT);
            general_reg.io_direction_reg.d2==0?pinMode(PB_2,INPUT):pinMode(PB_2,OUTPUT);
            general_reg.io_direction_reg.d3==0?pinMode(PB_3,INPUT):pinMode(PB_3,OUTPUT);
            //A口IO初始化-方向
            general_reg.io_direction_reg.a0==0?pinMode(PC_0,INPUT):pinMode(PC_0,OUTPUT);
            general_reg.io_direction_reg.a1==0?pinMode(PC_1,INPUT):pinMode(PC_1,OUTPUT);
            general_reg.io_direction_reg.a2==0?pinMode(PC_2,INPUT):pinMode(PC_2,OUTPUT);
            general_reg.io_direction_reg.a3==0?pinMode(PC_3,INPUT):pinMode(PC_3,OUTPUT);
            
            log::debug("0x44->write:%x\r\n",general_reg.io_direction_reg.value);
        }
        break;
        case 0x45://IO类型-PWM-AnalogIn-DIO-LEVEL
        {
            general_reg.io_type_reg.value = data[1];
        }
        break;
        case 0x46://IO电压
        {
            general_reg.io_voltage_reg.value = data[1];
            _sn74ahc595.updateRegisters();
        }
        break;
        case 0x47://条件动作使能
        {
            general_reg.condition_action_enable_reg.value = data[1];
            // ca0
            if(general_reg.condition_action_enable_reg.ca0==1){
                _logic_conditions[0].state = ActionState::Ready;
            }else{
                _logic_conditions[0].state = ActionState::Inactive;
                general_reg.slave_interrupt_reg.ca0 = 0;
            }
            // ca1
            if(general_reg.condition_action_enable_reg.ca1==1){
                _logic_conditions[1].state = ActionState::Ready;
            }else{
                _logic_conditions[1].state = ActionState::Inactive;
                general_reg.slave_interrupt_reg.ca1 = 0;
            }
            // ca2
            if(general_reg.condition_action_enable_reg.ca2==1){
                _logic_conditions[2].state = ActionState::Ready;
            }else{
                _logic_conditions[2].state = ActionState::Inactive;
                general_reg.slave_interrupt_reg.ca2 = 0;
            }
            // ca3
            if(general_reg.condition_action_enable_reg.ca3==1){
                _logic_conditions[3].state = ActionState::Ready;
            }else{
                _logic_conditions[3].state = ActionState::Inactive;
                general_reg.slave_interrupt_reg.ca3 = 0;
            }
            // ca4
            if(general_reg.condition_action_enable_reg.ca4==1){
                _logic_conditions[4].state = ActionState::Ready;
            }else{
                _logic_conditions[4].state = ActionState::Inactive;
                general_reg.slave_interrupt_reg.ca4 = 0;
            }
            // ca5
            if(general_reg.condition_action_enable_reg.ca5==1){
                _logic_conditions[5].state = ActionState::Ready;
            }else{
                _logic_conditions[5].state = ActionState::Inactive;
                general_reg.slave_interrupt_reg.ca5 = 0;
            }
            // ca6
            if(general_reg.condition_action_enable_reg.ca6==1){
                _logic_conditions[6].state = ActionState::Ready;
            }else{
                _logic_conditions[6].state = ActionState::Inactive;
                general_reg.slave_interrupt_reg.ca6 = 0;
            }
            // ca7
            if(general_reg.condition_action_enable_reg.ca7==1){
                _logic_conditions[7].state = ActionState::Ready;
            }else{
                _logic_conditions[7].state = ActionState::Inactive;
                general_reg.slave_interrupt_reg.ca7 = 0;
            }
        }
        break;
        case 0x48://保留
        break;
        case 0x49:// A口高低电平
        {
            general_reg.aio_level_reg.value = data[1];
            if(general_reg.io_direction_reg.d0==1){
                _ioport.aio0().write(general_reg.aio_level_reg.a0);
            }
            
            if(general_reg.io_direction_reg.d1==1){
                _ioport.aio1().write(general_reg.aio_level_reg.a1);
            }
            if(general_reg.io_direction_reg.d2==1){
                _ioport.aio2().write(general_reg.aio_level_reg.a2);
            }
            if(general_reg.io_direction_reg.d3==1){
                _ioport.aio3().write(general_reg.aio_level_reg.a3); 
            }
            log::debug("0x49->write:dir:0x%x:->level:0x:%x\r\n",general_reg.io_direction_reg.value,general_reg.aio_level_reg.value);
        }
        break;
        case 0x4A:// D口高低电平
        {
            general_reg.dio_level_reg.value = data[1];
            //_dio_port.d0().output();
            _ioport.dio0() = general_reg.dio_level_reg.d0;
            _ioport.dio1() = general_reg.dio_level_reg.d1;
            _ioport.dio2() = general_reg.dio_level_reg.d2;
            _ioport.dio3() = general_reg.dio_level_reg.d3; 
            log::debug("0x4A->write:0x%x\r\n",general_reg.dio_level_reg.value);
        }
        break;
        case 0x4B://只读---  A口模拟量:0x4B-0x52
        {
            
        }
        break;
        case 0x54://  D口PWM:0x54-0x58
        {
            general_reg.pwm_reg.freq.value         = data[1];
            PwmFreqName freq = (PwmFreqName)general_reg.pwm_reg.freq.value;
            if(general_reg.io_type_reg.d0 == 1){
                _ioport.pwm0().set_frequency(freq,general_reg.pwm_reg.duty_cycle.d0*1.0/100);
            }
            if(general_reg.io_type_reg.d1 == 1){
                _ioport.pwm1().set_frequency(freq,general_reg.pwm_reg.duty_cycle.d1*1.0/100);
            }
            if(general_reg.io_type_reg.d2 == 1){
                _ioport.pwm2().set_frequency(freq,general_reg.pwm_reg.duty_cycle.d2*1.0/100);
            }
            if(general_reg.io_type_reg.d3 == 1){
               _ioport.pwm3().set_frequency(freq,general_reg.pwm_reg.duty_cycle.d3*1.0/100);
            }
        }
        break;
        case 0x55://  D口PWM_duty0:0x55
        {
            general_reg.pwm_reg.duty_cycle.data[0] = data[1];
            _ioport.pwm0().write(general_reg.pwm_reg.duty_cycle.d0*1.0/100);
        }
        break;
         case 0x56://  D口PWM_duty0:0x55
        {
            general_reg.pwm_reg.duty_cycle.data[1] = data[1];
             _ioport.pwm1().write(general_reg.pwm_reg.duty_cycle.d1*1.0/100);
        }
        break;
        case 0x57://  D口PWM_duty0:0x55
        {   
            general_reg.pwm_reg.duty_cycle.data[2] = data[1];
            _ioport.pwm2().write(general_reg.pwm_reg.duty_cycle.d2*1.0/100);
        }
        break;
        case 0x58://  D口PWM_duty0:0x55
        { 
            general_reg.pwm_reg.duty_cycle.data[3] = data[1];
             _ioport.pwm3().write(general_reg.pwm_reg.duty_cycle.d3*1.0/100);
        }
        break;
        case 0x5B://  INPUT条件:0x5B-5C
        {
            general_reg.general_input_condition_reg.conditions[0].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[0].data[1] = data[2];
        }
        break;
        case 0x5D://  INPUT条件:0x5D-5E
        {
            general_reg.general_input_condition_reg.conditions[1].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[1].data[1] = data[2];
        }
        break;
        case 0x5F://  INPUT条件:0x5F-60
        {
            general_reg.general_input_condition_reg.conditions[2].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[2].data[1] = data[2];
        }
        break;
        case 0x61://  INPUT条件:0x61-62
        {
            general_reg.general_input_condition_reg.conditions[3].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[3].data[1] = data[2];
        }
        break;  
        case 0x63://  INPUT条件:0x63-64
        {
            general_reg.general_input_condition_reg.conditions[4].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[4].data[1] = data[2];
        }
        break;
        case 0x65://  INPUT条件:0x65-66
        {
            general_reg.general_input_condition_reg.conditions[5].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[5].data[1] = data[2];
        }
        break;
        case 0x67://  INPUT条件:0x67-68
        {
            general_reg.general_input_condition_reg.conditions[6].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[6].data[1] = data[2];
        }
        break;
        case 0x69://  INPUT条件:0x69-6A
        {
            general_reg.general_input_condition_reg.conditions[7].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[7].data[1] = data[2];
        }
        break;
        case 0x6B://  INPUT条件:0x6B-6C
        {
            general_reg.general_input_condition_reg.conditions[8].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[8].data[1] = data[2];
        }
        break;
        case 0x6D://  INPUT条件:0x6D-6E
        {
            general_reg.general_input_condition_reg.conditions[9].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[9].data[1] = data[2];
        }
        break;
        case 0x6F://  INPUT条件:0x6F-70
        {
            general_reg.general_input_condition_reg.conditions[10].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[10].data[1] = data[2];
        }
        break;
        case 0x71://  INPUT条件:0x71-72
        {
            general_reg.general_input_condition_reg.conditions[11].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[11].data[1] = data[2];
        }
        break;
        case 0x73://  INPUT条件:0x73-74
        {
            general_reg.general_input_condition_reg.conditions[12].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[12].data[1] = data[2];
        }
        break;
        case 0x75://  INPUT条件:0x75-76
        {
            general_reg.general_input_condition_reg.conditions[13].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[13].data[1] = data[2];
        }
        break;
        case 0x77://  INPUT条件:0x77-78
        {
            general_reg.general_input_condition_reg.conditions[14].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[14].data[1] = data[2];
        }
        break;
        case 0x79://  INPUT条件:0x79-7A
        {
            general_reg.general_input_condition_reg.conditions[15].data[0] = data[1];
            general_reg.general_input_condition_reg.conditions[15].data[1] = data[2];
        }
        break;
        case 0x7B:
        case 0x7C://  OUTPUT动作:0x7B-7C   -0
        {
            general_reg.general_output_action_reg.actions[0].data[0]= data[1];
            general_reg.general_output_action_reg.actions[0].data[1]= data[2];
        }
        break;
        case 0x7D:
        case 0x7E://  OUTPUT动作:7D-7E    -1
        {
            general_reg.general_output_action_reg.actions[1].data[0]= data[1];
            general_reg.general_output_action_reg.actions[1].data[1]= data[2];
        }
        break;
        case 0x7F:
        case 0x80://  OUTPUT动作:7F-80    -2
        {
            general_reg.general_output_action_reg.actions[2].data[0]= data[1];
            general_reg.general_output_action_reg.actions[2].data[1]= data[2];
        }
        break;
        case 0x81:
        case 0x82://  OUTPUT动作:81-82    -3
        {
            general_reg.general_output_action_reg.actions[3].data[0]= data[1];
            general_reg.general_output_action_reg.actions[3].data[1]= data[2];
        }
        break;
        case 0x83:
        case 0x84://  OUTPUT动作:83-84    -4
        {
            general_reg.general_output_action_reg.actions[4].data[0]= data[1];
            general_reg.general_output_action_reg.actions[4].data[1]= data[2];
        }
        break;
        case 0x85:
        case 0x86://  OUTPUT动作:85-86    -5
        {
            general_reg.general_output_action_reg.actions[5].data[0]= data[1];
            general_reg.general_output_action_reg.actions[5].data[1]= data[2];
        }
        break;
        case 0x87:
        case 0x88://  OUTPUT动作:87-88    -6
        {
            general_reg.general_output_action_reg.actions[6].data[0]= data[1];
            general_reg.general_output_action_reg.actions[6].data[1]= data[2];
        }
        break;
        case 0x89:
        case 0x8A://  OUTPUT动作:89-8A    -7
        {
            general_reg.general_output_action_reg.actions[7].data[0]= data[1];
            general_reg.general_output_action_reg.actions[7].data[1]= data[2];
        }
        break;
        case 0x8B:
        case 0x8C://  OUTPUT动作:8B-8C    -8
        {
            general_reg.general_output_action_reg.actions[8].data[0]= data[1];
            general_reg.general_output_action_reg.actions[8].data[1]= data[2];
        }
        break;
        case 0x8D:
        case 0x8E://  OUTPUT动作:8D-8E    -9
        {
            general_reg.general_output_action_reg.actions[9].data[0]= data[1];
            general_reg.general_output_action_reg.actions[9].data[1]= data[2];
        }
        break;
        case 0x8F:
        case 0x90://  OUTPUT动作:8F-90    -10
        {
            general_reg.general_output_action_reg.actions[10].data[0]= data[1];
            general_reg.general_output_action_reg.actions[10].data[1]= data[2];
        }
        break;
        case 0x91:
        case 0x92://  OUTPUT动作:91-92    -11
        {
            general_reg.general_output_action_reg.actions[11].data[0]= data[1];
            general_reg.general_output_action_reg.actions[11].data[1]= data[2];
        }
        break;
        case 0x93:
        case 0x94://  OUTPUT动作:93-94    -12
        {
            general_reg.general_output_action_reg.actions[12].data[0]= data[1];
            general_reg.general_output_action_reg.actions[12].data[1]= data[2];
        }
        break;
        case 0x95:
        case 0x96://  OUTPUT动作:95-96    -13
        {
            general_reg.general_output_action_reg.actions[13].data[0]= data[1];
            general_reg.general_output_action_reg.actions[13].data[1]= data[2];
        }
        break;
        case 0x97:
        case 0x98://  OUTPUT动作:97-98    -14
        {
            general_reg.general_output_action_reg.actions[14].data[0]= data[1];
            general_reg.general_output_action_reg.actions[14].data[1]= data[2];
        }
        break;
        case 0x99:
        case 0x9A://  OUTPUT动作:99-9A    -15
        {
            general_reg.general_output_action_reg.actions[15].data[0]= data[1];
            general_reg.general_output_action_reg.actions[15].data[1]= data[2];
        }
        break;
        case 0x9B://  时间条件:0x9B-0x9C-9D
        {
            general_reg.general_time_condition_reg.conditions[0].data[0] = data[1];
            general_reg.general_time_condition_reg.conditions[0].data[1] = data[2];
            general_reg.general_time_condition_reg.conditions[0].data[2] = data[3];
        }
        break;
        case 0x9E://  时间条件:0x9B-0xB2
        {
            general_reg.general_time_condition_reg.conditions[1].data[0] = data[1];
            general_reg.general_time_condition_reg.conditions[1].data[1] = data[2];
            general_reg.general_time_condition_reg.conditions[1].data[2] = data[3];
        }
        break;
        case 0xA1://  时间条件:0x9B-0xB2
        {
            general_reg.general_time_condition_reg.conditions[2].data[0] = data[1];
            general_reg.general_time_condition_reg.conditions[2].data[1] = data[2];
            general_reg.general_time_condition_reg.conditions[2].data[2] = data[3];
        }
        break;
        case 0xA4://  时间条件:0x9B-0xB2
        {
            general_reg.general_time_condition_reg.conditions[3].data[0] = data[1];
            general_reg.general_time_condition_reg.conditions[3].data[1] = data[2];
            general_reg.general_time_condition_reg.conditions[3].data[2] = data[3];
        }
        break;
        case 0xA7://  时间条件:0x9B-0xB2
        {
            general_reg.general_time_condition_reg.conditions[4].data[0] = data[1];
            general_reg.general_time_condition_reg.conditions[4].data[1] = data[2];
            general_reg.general_time_condition_reg.conditions[4].data[2] = data[3];
        }
        break;
        case 0xAA://  时间条件:0x9B-0xB2
        {
            general_reg.general_time_condition_reg.conditions[5].data[0] = data[1];
            general_reg.general_time_condition_reg.conditions[5].data[1] = data[2];
            general_reg.general_time_condition_reg.conditions[5].data[2] = data[3];
        }
        break;
        case 0xAD://  时间条件:0x9B-0xB2
        {
            general_reg.general_time_condition_reg.conditions[6].data[0] = data[1];
            general_reg.general_time_condition_reg.conditions[6].data[1] = data[2];
            general_reg.general_time_condition_reg.conditions[6].data[2] = data[3];
        }
        break;
        case 0xB0://  时间条件:0x9B-0xB2
        {
            general_reg.general_time_condition_reg.conditions[7].data[0] = data[1];
            general_reg.general_time_condition_reg.conditions[7].data[1] = data[2];
            general_reg.general_time_condition_reg.conditions[7].data[2] = data[3];
        }
        break;
        case 0xB3://  条件和动作:0xB3-0xDA
        {
            for(uint8_t i=0;i<5;i++){
                general_reg.general_condition_and_action_reg.conditions[0].data[i] = data[1+i];
            }
        }
        break;
        case 0xB8://  条件和动作:0xB3-0xDA
        {
            for(uint8_t i=0;i<5;i++){
                general_reg.general_condition_and_action_reg.conditions[1].data[i] = data[1+i];
            }
        }
        break;
        case 0xBD://  条件和动作:0xB3-0xDA
        {
            for(uint8_t i=0;i<5;i++){
                general_reg.general_condition_and_action_reg.conditions[2].data[i] = data[1+i];
            }
        }
        break;
        case 0xC2://  条件和动作:0xB3-0xDA
        {
            for(uint8_t i=0;i<5;i++){
                general_reg.general_condition_and_action_reg.conditions[3].data[i] = data[1+i];
            }
        }
        break;
        case 0xC7://  条件和动作:0xB3-0xDA
        {
            for(uint8_t i=0;i<5;i++){
                general_reg.general_condition_and_action_reg.conditions[4].data[i] = data[1+i];
            }
        }
        break;
        case 0xCC://  条件和动作:0xB3-0xDA
        {
            for(uint8_t i=0;i<5;i++){
                general_reg.general_condition_and_action_reg.conditions[5].data[i] = data[1+i];
            }
        }
        break;
        case 0xD1://  条件和动作:0xB3-0xDA
        {
            for(uint8_t i=0;i<5;i++){
                general_reg.general_condition_and_action_reg.conditions[6].data[i] = data[1+i];
            }
        }
        break;
        case 0xD6://  条件和动作:0xB3-0xDA
        {
            for(uint8_t i=0;i<5;i++){
                general_reg.general_condition_and_action_reg.conditions[7].data[i] = data[1+i];
            }
        }
        break;
         case 0xE1://输入条件-A0
        {
            for(uint8_t i=0;i<sizeof(general_reg.general_input_condition_reg.a0_data);i++){
                general_reg.general_input_condition_reg.a0_data[i]=data[1+i];
            }
        }
        break;
        case 0xE2://输入条件-A1
        {
            for(uint8_t i=0;i<sizeof(general_reg.general_input_condition_reg.a1_data);i++){
                general_reg.general_input_condition_reg.a1_data[i]=data[1+i];
            }
        }
        break;
        case 0xE3://输入条件-A2
        {
            for(uint8_t i=0;i<sizeof(general_reg.general_input_condition_reg.a2_data);i++){
                general_reg.general_input_condition_reg.a2_data[i]=data[1+i];
            }
        }
        break;
        case 0xE4://输入条件-A3
        {
            for(uint8_t i=0;i<sizeof(general_reg.general_input_condition_reg.a3_data);i++){
                general_reg.general_input_condition_reg.a3_data[i]=data[1+i];
            }
        }
        break;  
         case 0xE5://输出动作-D0
        {
            for(uint8_t i=0;i<sizeof(general_reg.general_output_action_reg.dio[1].data);i++){
               general_reg.general_output_action_reg.dio[0].data[i]=data[1+i];
            }
        }
        break;
        case 0xE6://输出动作-D1
        {
            for(uint8_t i=0;i<sizeof(general_reg.general_output_action_reg.dio[1].data);i++){
               general_reg.general_output_action_reg.dio[1].data[i]=data[1+i];
            }
        }
        break;
        case 0xE7://输出动作-D2
        {
            for(uint8_t i=0;i<sizeof(general_reg.general_output_action_reg.dio[2].data);i++){
               general_reg.general_output_action_reg.dio[2].data[i]=data[1+i];
            }
        }
        break;
        case 0xE8://输出动作-D3
        {
            for(uint8_t i=0;i<sizeof(general_reg.general_output_action_reg.dio[3].data);i++){
               general_reg.general_output_action_reg.dio[3].data[i]=data[1+i];
            }
        }
        break;
        default:
        break;
    }
}
void DispatcherHandler::cleanup()
{

}

bool DispatcherHandler::apply_input_compare(uint8_t condition_id,uint8_t input_condition_id)
{   
    
    general_condition_and_action_reg_t& general_condition_and_action_reg = DataStorageIO::module_reg().module_general_reg.general_condition_and_action_reg;
    uint8_t num = general_condition_and_action_reg.conditions[condition_id].logic_input_condition[input_condition_id].num;
    input_condition_details_t& amic = DataStorageIO::module_reg().module_general_reg.general_input_condition_reg.conditions[num];
    
    module_general_reg_t& general_reg = DataStorageIO::module_reg().module_general_reg;
    //读取端口
    switch(amic.pin_name){
        case 0:
        {
            //判断IO方向为输出直接返回
            if(general_reg.io_direction_reg.a0==1)
            {
                return false;
            }
            if(general_reg.io_type_reg.a0 == IOTypeName::ANALOG_IN){
                _logic_conditions[condition_id].input_voltage[input_condition_id] = _ioport.ain0().read_u16();
            }else{
                _logic_conditions[condition_id].input_voltage[input_condition_id] = _ioport.aio0().read();
            }
        }
        break;
        case 1:
        {
            if(general_reg.io_direction_reg.a1==1)
            {
                return false;
            }
            if(general_reg.io_type_reg.a1 == IOTypeName::ANALOG_IN){
                _logic_conditions[condition_id].input_voltage[input_condition_id] = _ioport.ain1().read_u16();
            }else{
                    _logic_conditions[condition_id].input_voltage[input_condition_id] = _ioport.aio1().read();
            }
        }
        break;
        case 2:
        {   
            if(general_reg.io_direction_reg.a2==1)
            {
                return false;
            }
            if(general_reg.io_type_reg.a2 == IOTypeName::ANALOG_IN){
                _logic_conditions[condition_id].input_voltage[input_condition_id] = _ioport.ain2().read_u16();
            }else{
                _logic_conditions[condition_id].input_voltage[input_condition_id] = _ioport.aio2().read();
            }
        }
        break;
        case 3:
        {
            if(general_reg.io_direction_reg.a3==1)
            {
                return false;
            }
            if(general_reg.io_type_reg.a3 == IOTypeName::ANALOG_IN){
                _logic_conditions[condition_id].input_voltage[input_condition_id] = _ioport.ain3().read_u16();
            }else{
                _logic_conditions[condition_id].input_voltage[input_condition_id] = _ioport.aio3().read();
            }
        }
        break;
        default:break;
    } 
    //执行比较
    bool ok = false;
    switch(amic.operate_symbol)
    {
        case OPERATE_SYMBOL_LT://符号:<
        {
            if(_logic_conditions[condition_id].input_voltage[input_condition_id] < amic.threshold){
                ok = true;
            }
        }
        break;
        case OPERATE_SYMBOL_EQ://符号:=
        {
            if(_logic_conditions[condition_id].input_voltage[input_condition_id] == amic.threshold){
                ok = true;
            }
        }
        break;
        case OPERATE_SYMBOL_GT://符号:>
        {
            if(_logic_conditions[condition_id].input_voltage[input_condition_id] > amic.threshold){
                ok = true;
            }
        }
        break;
        case OPERATE_SYMBOL_LE://符号:<=
        {
            if(_logic_conditions[condition_id].input_voltage[input_condition_id] <= amic.threshold){
                ok = true;
            }
        }
        break;
        case OPERATE_SYMBOL_GE://符号:>=
        {
            if(_logic_conditions[condition_id].input_voltage[input_condition_id] >= amic.threshold){
                ok = true;
            }  
        }
        break;
        case OPERATE_SYMBOL_NE://符号:!=
        {
            if(_logic_conditions[condition_id].input_voltage[input_condition_id] != amic.threshold){
                ok = true;
            }
        }
        break;
        default:break;
    }

    return ok;
}
bool DispatcherHandler::apply_time_compare(uint8_t condition_id,uint8_t time_condition_id)
{   
    general_condition_and_action_reg_t& general_condition_and_action_reg = DataStorageIO::module_reg().module_general_reg.general_condition_and_action_reg;
    uint8_t num = time_condition_id==0?general_condition_and_action_reg.conditions[condition_id].logic_time_condition.sn0:general_condition_and_action_reg.conditions[condition_id].logic_time_condition.sn1;

    auto_mode_time_condition_t& amtc = DataStorageIO::module_reg().module_general_reg.general_time_condition_reg.conditions[num];

    bool ok = false;
    unsigned long t = (millis()-_logic_conditions[condition_id].last_millis[time_condition_id])/1000;

    // if(condition_id==1 && time_condition_id==0){
    //     log::debug("CA1:time:%lu->%lu->%d\r\n",t,amtc.threshold,amtc.operate_symbol);
    // }
    switch(amtc.operate_symbol)
    {
        case OPERATE_SYMBOL_LT:
        {
           if( t < amtc.threshold ){
              ok = true;
           }
        }
        break;
        case OPERATE_SYMBOL_EQ:
        {
            if( t == amtc.threshold ){
              ok = true;
            }
        }
        break;
        case OPERATE_SYMBOL_GT:
        {
            if( t > amtc.threshold ){
              ok = true;
            }
        }
        break;
        case OPERATE_SYMBOL_LE:
        {
            if( t <= amtc.threshold ){
              ok = true;
            }
        }
        break;
        case OPERATE_SYMBOL_GE:
        {
            if( t >= amtc.threshold ){
              ok = true;
            }
        }
        break;
        case OPERATE_SYMBOL_NE:
        {
            if( t != amtc.threshold ){
              ok = true;
            }
        }
        break;
        default:break;
    }
    return ok;
}



bool DispatcherHandler::apply_condition_compare(uint8_t condition_id)
{
    bool ok = false;

    switch(_logic_conditions[condition_id].state)
    {
        case ActionState::Inactive:
        break;
        case ActionState::Ready:
        {
            _logic_conditions[condition_id].last_millis[0] = millis();
            _logic_conditions[condition_id].last_millis[1] = millis();
            _logic_conditions[condition_id].state = ActionState::Running; 
        }
        break;
        case ActionState::Running:
        {
            general_condition_and_action_reg_t& general_condition_and_action_reg = DataStorageIO::module_reg().module_general_reg.general_condition_and_action_reg;

            bool en_ok[4]={general_condition_and_action_reg.conditions[condition_id].logic_input_condition[0].en,
                        general_condition_and_action_reg.conditions[condition_id].logic_input_condition[1].en,
                        general_condition_and_action_reg.conditions[condition_id].logic_time_condition.en0,
                        general_condition_and_action_reg.conditions[condition_id].logic_time_condition.en1};

            if( !(en_ok[0] || en_ok[1] || en_ok[2] || en_ok[3]) ){
                ok = false;
                _logic_conditions[condition_id].state = ActionState::Exit;
                break;
            }     
            
            
            //输入判断
            bool input_ok = false;
            if(en_ok[0]&&en_ok[1]){
                input_ok = this->apply_input_compare(condition_id,0) &&
                        this->apply_input_compare(condition_id,1);
            }else if(en_ok[0]){
                input_ok = this->apply_input_compare(condition_id,0);
            }else{
                input_ok = this->apply_input_compare(condition_id,1);
            }
           
            if(input_ok){
                ok = true;
                _logic_conditions[condition_id].state = ActionState::Exit;
                break;
            }

            //时间判断
            bool time_ok = false;

            if(en_ok[2]&&en_ok[3]){
                time_ok = this->apply_time_compare(condition_id,0) &&
                        this->apply_time_compare(condition_id,1);
            }else if(en_ok[2]){
                time_ok =  this->apply_time_compare(condition_id,0);
            }else{
                time_ok = this->apply_time_compare(condition_id,1);
            }
            if(time_ok){
                ok = true;
                _logic_conditions[condition_id].state = ActionState::Exit;
            }
            
        }
        break;
        //case ActionState::WaitingDelay:break;
        case ActionState::OutputAction:
        break;
        case ActionState::Exit:
        break;
        default:break;
    }
    
    return ok;
}

void DispatcherHandler::control_action_output(output_action_details_t& action)
{
    switch(action.pin_name)
        {
            case 0:
            {
                action.output_type == IOTypeName::PWM_OUT?_ioport.pwm0().set_frequency((PwmFreqName)action.freq_num,action.duty_cycle):
                                    _ioport.dio0().write(action.duty_cycle>0?1:0);
                                    log::debug("_ioport.dio0:%d\r\n",action.duty_cycle);
            }
            break;
            case 1:
            {
                action.output_type == IOTypeName::PWM_OUT?_ioport.pwm1().set_frequency((PwmFreqName)action.freq_num,action.duty_cycle):
                                    _ioport.dio1().write(action.duty_cycle>0?1:0);
            }
            break;
            case 2:
            {
                action.output_type == IOTypeName::PWM_OUT?_ioport.pwm2().set_frequency((PwmFreqName)action.freq_num,action.duty_cycle):
                                    _ioport.dio2().write(action.duty_cycle>0?1:0);
            }
            break;
            case 3:
            {
                action.output_type == IOTypeName::PWM_OUT?_ioport.pwm3().set_frequency((PwmFreqName)action.freq_num,action.duty_cycle):
                                    _ioport.dio3().write(action.duty_cycle>0?1:0);
            }
            break;
            default:break;
        }
}
void DispatcherHandler::apply_output_action(uint8_t condition_id)
{
   
    general_condition_and_action_reg_t& general_condition_and_action_reg = DataStorageIO::module_reg().module_general_reg.general_condition_and_action_reg;
    //输出判断
    if( !(general_condition_and_action_reg.conditions[condition_id].logic_output_action[0].en || general_condition_and_action_reg.conditions[condition_id].logic_output_action[1].en))
    {
        return;
    }

    general_output_action_reg_t& general_output_action_reg=DataStorageIO::module_reg().module_general_reg.general_output_action_reg;
    //动作0输出
    auto_mode_logic_output_action_t& output_action_0 = general_condition_and_action_reg.conditions[condition_id].logic_output_action[0];
    if(output_action_0.en){
        this->control_action_output(general_output_action_reg.actions[output_action_0.num]);
    }
    //动作1输出
    auto_mode_logic_output_action_t& output_action_1 = general_condition_and_action_reg.conditions[condition_id].logic_output_action[1];
    if(output_action_1.en){
        this->control_action_output(general_output_action_reg.actions[output_action_1.num]);
    }
}

void DispatcherHandler::clear_interrupt()
{
    DataStorageIO::module_reg().module_general_reg.slave_interrupt_reg.value= 0;
    _interrupt_pin = 0;
}
data_storage_reg_read_t& DispatcherHandler::get_reg_data(uint8_t reg_address)
{
    module_general_reg_t& module_general_reg = DataStorageIO::module_reg().module_general_reg;
    switch(reg_address)
    {
        case 0x41:
        {
            static uint8_t interrupt_data[1];
            interrupt_data[0] = module_general_reg.slave_interrupt_reg.value;
            _reg_read.data    =  interrupt_data;
            _reg_read.size    = 1;
        }
        break;
        default:
        {
            return _ds.get_reg_data(reg_address);
        }
        break;
    }
    
    return  _reg_read;
}